import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../providers/provider_container.dart';
import '../entity/video_tag_bean.dart';
import '../intent/video_tag_state.dart';
import '../model/video_tag_viewmodel.dart';

class VideoTagPage extends ConsumerStatefulWidget {
  final String json;

  VideoTagPage({required this.json});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() {
    return _VideoTagPageState();
  }
}

class _VideoTagPageState extends ConsumerState<VideoTagPage> {
  VideoTagViewModel get viewModel => ref.read(videoTagViewModelProvider.notifier);

  @override
  void initState() {
    super.initState();
    // 在页面初始化时执行网络请求
    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.initData(widget.json);
    });
  }

  @override
  Widget build(BuildContext context) {
    final VideoTagState state = ref.watch(videoTagViewModelProvider);
    return Scaffold(
      appBar: AppBar(title: Text('标签列表'), centerTitle: true),
      body: SafeArea(
        child: Container(
          width: double.infinity,
          height: double.infinity,
          child: CustomScrollView(
            slivers: [
              if (state.pageState.isNotEmpty) SliverToBoxAdapter(child: Center(child: Text(state.pageState))),
              // 第一个真正的可滚动列表 (SliverList)
              SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
                  VideoTagBean item = state.roomUiList[index];
                  return InkWell(
                    onTap: () {
                      viewModel.goNextPage(item.name);
                    },
                    child: Container(
                      child: Column(
                        children: [
                          SizedBox(height: 10),
                          Text(item.tag),
                          SizedBox(height: 10),
                          Divider(thickness: 1, indent: 10, endIndent: 10, color: Colors.grey),
                        ],
                      ),
                    ),
                  );
                }, childCount: state.roomUiList.length),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
